﻿using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Yglu.BusinessLayer;
using Yglu.BusinessLayer.Entities;

namespace Yglu.TerminalApp
{
	public partial class ProductsControl : UserControl
	{
		#region Properties

		public CategoryCollection Categories { get; set; }

		public Combo CurrentCombo
		{
			get
			{
				Combo c = new Combo();

				ListView.ListViewItemCollection items = listCombo.Items;

				foreach (ListViewItem i in items)
				{
					c.Products.Add((Product)i.Tag);
				}

				Promotion applyPromotion = null;

				foreach (Promotion p in Program.Promotions)
				{
					if (!p.Automatic)
					{
						continue;
					}

					if (p.Products > 0 && c.Products.Count >= p.Products)
					{
						applyPromotion = p;
					}
				}

				if (applyPromotion != null)
				{
					c.PromotionApplied = applyPromotion;
				}

				return c;
			}
		}

		#endregion

		#region Main methods

		public ProductsControl()
		{
			InitializeComponent();
		}

		public void LoadProducts()
		{
			tabProducts.TabPages.Clear();
			listCombo.LargeImageList = Program.ProductIcons;

			CategoryCollection categories = new CategoryCollection(DataAccess.Category_Get(Program.LoggedTerminal.Store));

			foreach (Category c in categories)
			{
				TabPage tab = new TabPage();
				tab.Dock = DockStyle.Fill;
				tab.Text = c.Title;

				ListView list = new ListView();
				list.Activation = ItemActivation.OneClick;
				list.AutoArrange = true;
				list.Alignment = ListViewAlignment.SnapToGrid;
				list.Cursor = Cursors.Hand;
				list.Dock = DockStyle.Fill;
				list.Font = new Font(Font.FontFamily, 10, Font.Style);
				list.HideSelection = false;
				list.LargeImageList = Program.ProductIcons;
				list.LabelEdit = false;
				list.LabelWrap = true;
				list.MultiSelect = c.MultiSelect;
				list.View = View.LargeIcon;

				list.SelectedIndexChanged += listProducts_SelectedIndexChanged;

				ProductCollection products = new ProductCollection(DataAccess.Product_Get(c));

				foreach (Product p in products)
				{
					ListViewItem i = new ListViewItem(p.Title);

					i.Tag = p;
					i.UseItemStyleForSubItems = true;

					if (!p.Category.MultiSelect)
					{
						i.Font = new Font(i.Font.FontFamily, i.Font.Size, FontStyle.Bold);
					}

					if (Program.ProductIcons.Images.ContainsKey(p.Id.ToString()))
					{
						i.ImageKey = p.Id.ToString();
					}
					else
					{
						i.ImageIndex = 0;
					}

					list.Items.Add(i);
				}

				tab.Controls.Add(list);
				tabProducts.TabPages.Add(tab);
			}
		}

		public void Reset()
		{
			listCombo.Items.Clear();

			foreach (TabPage tab in tabProducts.TabPages)
			{
				((ListView)tab.Controls[0]).SelectedItems.Clear();
			}

			tabProducts.SelectedIndex = 0;
		}

		#endregion

		#region Product selection

		private void listProducts_SelectedIndexChanged(Object sender, EventArgs e)
		{
			ListView list = (ListView)sender;
			ListView.SelectedListViewItemCollection items = list.SelectedItems;

			foreach (ListViewItem item in items)
			{
				Product p1 = (Product)item.Tag;

				if (!p1.Category.MultiSelect)
				{
					foreach (ListViewItem sel in listCombo.Items)
					{
						Product p2 = (Product)sel.Tag;

						if (p1.Category.Id == p2.Category.Id)
						{
							listCombo.Items.Remove(sel);
							break;
						}
					}
				}

				listCombo.Items.Add((ListViewItem)item.Clone());
				item.Selected = false;
			}
		}

		public String GetSelectedProductList()
		{
			StringBuilder sb = new StringBuilder();

			foreach (TabPage tab in tabProducts.TabPages)
			{
				ListView list = ((ListView)tab.Controls[0]);

				foreach (ListViewItem item in list.SelectedItems)
				{
					Product p = (Product)item.Tag;

					sb.Append(p.FullTitle);
					sb.Append(Environment.NewLine);
				}
			}

			if (sb.Length < 3)
			{
				sb.Append(Messages.Empty);
			}

			return Messages.CurrentCombo + Environment.NewLine + sb;
		}

		#endregion

		#region Remove product

		private void imgRemove_Click(object sender, EventArgs e)
		{
			if (listCombo.Items.Count == 1)
			{
				listCombo.Items.Clear();
				return;
			}

			for (Int32 i = listCombo.SelectedItems.Count - 1; i >= 0; i--)
			{
				listCombo.Items.Remove(listCombo.SelectedItems[i]);
			}
		}

		#endregion
	}
}